package cn.school.mybatis.servlet;


import cn.school.mybatis.Util.SpringMButil;
import cn.school.mybatis.dao.GradeDao;
import cn.school.mybatis.pojo.Grade;
import cn.school.mybatis.pojo.Student;
import cn.school.mybatis.service.GradeService;
import cn.school.mybatis.service.StudentService;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/doStudentInfoShowServlet")
public class doStudentInfoShowServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();

        int num = new Integer(req.getParameter("stuno"));
        StudentService studentService= SpringMButil.getBean(StudentService.class);
        Student student = studentService.get(num);
        if (student == null) {
            out.print("<script>alert('未找到相关信息！');history.back();</script>");
            return;
        }

        //年级名称
        GradeService gradeService= SpringMButil.getBean(GradeService.class);
        List<Grade> lists = gradeService.searchAll();
//        String gradeName=lists.get(student.getGradeid()).getGname();
        String gradeName=gradeService.get(student.getGradeid()).getGname();
        student.setGname(gradeName);
        System.out.println(student);

        req.setAttribute("student",student);
        req.setAttribute("gradeName",gradeName);

        req.getRequestDispatcher("/WEB-INF/AdminLTEdemo01/s_student/s_studentShow.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }
}
